package com.sise.cn.utils;

import com.alibaba.fastjson.JSONObject;
import com.sise.cn.enums.error.SystemError;
import com.sise.cn.exception.SystemException;

/**
 * 暂时保存在threadlocal中
 *
 * @author CCJ
 */
public class SessionContextHolder {

    private static ThreadLocal<JSONObject> context = ThreadLocal.withInitial(JSONObject::new);

    public static void setToken(String token) {
        context.get().put("token", token);
    }

    public static void setOpenId(String openId) {
        context.get().put("openId", openId);
    }

    public static void getOpenId() {
        context.get().getString("openId");
    }

    public static void setLoginAccountInfo(LoginAccountInfo accountInfo) {
        context.get().put("loginAccountInfo", accountInfo);
    }

    private static LoginAccountInfo getLoginAccountInfo() {
        return (LoginAccountInfo) context.get().get("loginAccountInfo");
    }

    public static LoginAccountInfo getAccountAndValid() {
        LoginAccountInfo accountInfo = getLoginAccountInfo();
        if (null == accountInfo) {
            throw new SystemException(SystemError.UNKNOWN_ERROR, "login throught check but cannot find accountInfo");
        }
        return accountInfo;
    }
}
